package com.passion.fs.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import com.passion.context.StdServiceContext;
import com.passion.domain.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 类描述：网页拦截器
 * 进入某个页面之前进行一些参数的设置
 * @author ZhuYin
 * @since 2025年01月10日
 */

@Slf4j
@RefreshScope
@Component
public class WebPageInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 获取 application.yml 中配置的 contextPath
        String contextPath = request.getContextPath();
        request.setAttribute("contextPath", contextPath);
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("version", "1.0.0");
            if (StpUtil.isLogin()) {
                // 登录用户信息
                SysUser sysUser = StdServiceContext.sysUserService.getById(StpUtil.getLoginIdAsString());
                sysUser.setUserPasswd("");
                sysUser.setUserSalt("");
                session.setAttribute("loginUser", sysUser);
            }
        }
        return true;
    }
}
